package com.henanuniversity.service.impl;

import com.henanuniversity.dao.BusinessDao;
import com.henanuniversity.dao.CartDao;
import com.henanuniversity.dao.OrdersDao;
import com.henanuniversity.dao.impl.*;
import com.henanuniversity.po.Business;
import com.henanuniversity.po.Cart;
import com.henanuniversity.po.OrderDetailet;
import com.henanuniversity.po.Orders;
import com.henanuniversity.service.OrdersService;
import com.henanuniversity.utils.DBUtils;

import java.util.ArrayList;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    @Override
    public int createOrders(String userId,Integer businessId,Integer daId) {
        int orderId = 0;
        BusinessDao businessDao = new BusinessDaoImpl();
        CartDao cartDao = new CartDaoImpl();
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderDetailetDao orderDetailetDao = new OrderDetailetDaoImpl();
        try {
            DBUtils.beginTransaction();  //开启事务
            //connection.setAutocommit(false);
            //1、查询当前用户购物车中当前商家的所有食品
            Cart cart = new Cart();
            cart.setBusinessId(businessId);
            cart.setUserId(userId);
            List<Cart> cartList = cartDao.listCart(cart);
            //2、查询商家信息（需要使用商家的配送费信息）
            Business business = businessDao.getBusinessById(businessId);
            //3、获取订单总额
            Double ordersTotal = 0.0;  //订单总额
            for(Cart c : cartList) {
                //累计所有食品总价格
                ordersTotal += c.getFood().getFoodPrice()*c.getQuantity();
            }
            //加上配送费
            ordersTotal += business.getDeliveryPrice();
            //3、创建订单，并获取订单编号
            Orders orders = new Orders();
            orders.setUserId(userId);
            orders.setBusinessId(businessId);
            orders.setOrderTotal(ordersTotal);
            orders.setDaId(daId);
            //保存订单
            orderId = ordersDao.saveOrders(orders);

            //4、处理相关数据：获取订单明细集合
            List<OrderDetailet> orderDetailetList = new ArrayList<>();  //订单明细集合
            for(Cart c : cartList) {
                OrderDetailet od = new OrderDetailet();
                od.setOrderId(orderId);
                od.setFoodId(c.getFoodId());
                od.setQuantity(c.getQuantity());
                orderDetailetList.add(od);
            }
            //5、批量添加订单明细
            //if(!(orderDetailetList.isEmpty())){

                orderDetailetDao.saveOrderDetailetBatch(orderDetailetList); //  //保存订单明细
           // }
            //6、清空当前用户购物车中当前商家的所有食品
            cartDao.removeCart(cart);
            //connection.commit()
            DBUtils.commitTransaction();  //提交事务
        } catch (Exception e) {
            //connection.rollback()
            orderId = 0;
            try {
                DBUtils.rollbackTransaction();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        DBUtils.close();
        return orderId;
    }
    @Override
    public Orders getOrdersById(Integer orderId) {
        Orders orders = null;
        OrdersDao dao = new OrdersDaoImpl();
        OrderDetailetDao orderDetailetDao = new OrderDetailetDaoImpl();
        try {

            orders = dao.getOrdersById(orderId);
            List<OrderDetailet> list = orderDetailetDao.listOrderDetailetByOrderId(orderId);
            orders.setOdList(list);
        } catch(Exception e) {
            e.printStackTrace();
        }
        DBUtils.close();
        return orders;
    }

    @Override
    public List<Orders> listOrderByUserId(String userId) {
       List<Orders> list= new ArrayList<>();
        try {
            OrdersDao ordersDao=new OrdersDaoImpl() ;
            OrderDetailetDao orderDetailetDao=new OrderDetailetDaoImpl();
            DBUtils.getConnection();
            System.out.println("service:"+userId);
            list=ordersDao.listOrderByUserId(userId);
            for(Orders o:list){
                List<OrderDetailet> odList=orderDetailetDao.listOrderDetailetByOrderId(o.getOrderId());
                o.setOdList(odList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtils.close();
        }
        return list;
    }
}
